#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e3+10;
typedef long long LL;
inline int read(){int x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}	return x*f;}
bool vis[maxn][maxn];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
LL a[maxn][maxn];
bool valley=1;
bool hall=1;
LL n,m;
void dfs(LL x,LL y){
    vis[x][y]=1;
    for(LL i=0;i<4;i++){
        LL nxx=x+dx[i];
        LL nyy=y+dy[i];
        if(nxx<1||nxx>n||nyy<1||nyy>m) continue;
        if(a[nxx][nyy]<a[x][y]){
            valley=0;
        }
        if(a[nxx][nyy]>a[x][y]){
            hall=0;
        }
        if(a[nxx][nyy]==a[x][y]&&!vis[nxx][nyy]) dfs(nxx,nyy);
    }
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  n=read();m=read();
  for(LL i=1;i<=n;i++){
    for(LL j=1;j<=m;j++){
        a[i][j]=read();
    }
  }
  LL sum1=0;LL sum2=0;
  for(LL i=1;i<=n;i++){
    for(LL j=1;j<=m;j++){
        if(!vis[i][j]){
        ///    cout<<"i="<<i<<"  "<<"j="<<j<<endl;
            dfs(i,j);
            if(valley==1) sum1++;
            if(hall==1) sum2++;
            valley=1;hall=1;
        }
    }
  }
  cout<<sum1<<" "<<sum2<<endl;
return 0;
}
